package com.lfg.leetcode.leetcode100;

public class No121maxProfit {

    public static void main(String[] args) {
        int[] data = new int[]{7, 1, 5, 3, 6, 4};
        int ret = maxProfit(data);
        System.out.println("[" + ret + "]");
    }


    public static int maxProfit(int[] prices) {
        if (prices.length < 2) {
            return 0;
        }
        int ret = 0;
        int minIndex = 0;
        for (int i = 1; i < prices.length; i++) {
            if (prices[i] < prices[minIndex]) {
                minIndex = i;
            }

            if (prices[i] - prices[minIndex] > ret) {
                ret = prices[i] - prices[minIndex];
            }
        }
        return ret;
    }
}
